package com.onemile.app;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onemile.app.base.BaseUI;
import com.onemile.app.base.BaseUINoBottom;
import com.onemile.data.DXml;
import com.onemile.entity.MessageCode;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.entity.model.EUser;
import com.onemile.net.NetClient;
import com.onemile.utils.UtiDialogsFactory;

public class UIRegister extends BaseUINoBottom
{
   private EditText edtName;
   private EditText edtPwd;
   private EditText edtPwdAgain;
   private EditText edtMobile;
   private ImageView imgRegister;

   private void InitControls()
   {
	  edtName = (EditText) findViewById(R.id.edtUserName_register);
	  edtPwd = (EditText) findViewById(R.id.edtPwd_register);
	  edtPwdAgain = (EditText) findViewById(R.id.edtPwdAgain_register);
	  edtMobile = (EditText) findViewById(R.id.edtMoblie_register);
	  imgRegister = (ImageView) findViewById(R.id.imgRegister_register);
	  imgRegister.setOnClickListener(new View.OnClickListener()
	  {
		 @Override
		 public void onClick(View v)
		 {
			if (VerifyInfo())
			{
			   executeAsyncTask(PubConst.TaskName.REGISTER);
			}
		 }
	  });
   }

   private boolean VerifyInfo()
   {
	  if (edtName.getText().toString().trim() == null || edtName.getText().toString().trim().equals(""))// 用户名为空
	  {
		 UtiDialogsFactory.getInstance().ShowToastInfoShort(UIRegister.this, getString(R.string.login_user_not_empty));
		 return false;
	  }
	  else if (edtPwd.getText().toString().trim() == null || edtPwd.getText().toString().trim().equals(""))// 密码为空
	  {
		 UtiDialogsFactory.getInstance().ShowToastInfoShort(UIRegister.this, getString(R.string.login_pwd_not_empty));
		 return false;
	  }
	  else if (!edtPwdAgain.getText().toString().trim().equals(edtPwd.getText().toString().trim()))// 密码一样
	  {
		 UtiDialogsFactory.getInstance().ShowToastInfoShort(UIRegister.this, getString(R.string.login_pwd_not_same));
		 return false;
	  }
	  else if (edtMobile.getText().toString().trim() == null || edtMobile.getText().toString().trim().equals(""))// 邮件为空
	  {
		 UtiDialogsFactory.getInstance().ShowToastInfoShort(UIRegister.this, getString(R.string.login_mobile_no_empty));
		 return false;
	  }
	  return true;
   }

   @Override
   protected void onCreateOverride(Bundle savedInstanceState)
   {
	  InitControls();
   }

   @Override
   public int SetContentViewLayoutResId()
   {
	  return R.layout.register;
   }

   @Override
   public void SetTopToolbar(TextView topTxt, ImageView leftImBtn, ImageView rightImgBtn, RelativeLayout rightRay, TextView cartNum)
   {
	  topTxt.setText(getString(R.string.title_register));
   }

   @Override
   public void onAsyncTaskStart(String taskName, Object[] params)
   {
	  UtiDialogsFactory.getInstance().ShowLoadingDialogNoTitle(UIRegister.this, getString(R.string.dialog_loading));
   }

   @Override
   public Object onAsyncTaskInBackground(String taskName, Object[] params) throws Exception
   {
	  if (taskName.equals(PubConst.TaskName.REGISTER))
	  {
		 try
		 {
			EUser user=new EUser();
			user.setUserName(edtName.getText().toString().trim());
			user.setPassword(edtPwd.getText().toString().trim());
			user.setMobile(edtMobile.getText().toString().trim());
            return NetClient.Register(user);
		 }
		 catch (Exception e)
		 {
			if (Session.getInstance().IsDEBUG())
			{
			   Log.e("ERROR", "UIRegister_onAsyncTaskInBackground(taskName, params) ", e);
			}
		 }
	  }
	  return null;
   }

   @Override
   public void onAsyncTaskEnd(String taskId, String taskName, Object[] params, Object result)
   {
	  if(taskName.equals(PubConst.TaskName.REGISTER))
	  {
		 try
		 {
			UtiDialogsFactory.getInstance().DismissLoadingDialog();
			if (result != null)
			{
			   DXml data = (DXml) result;
               if(data.getHead().GetFieldValue("RspCode").equals(MessageCode.Success.Code))
               {
                  UtiDialogsFactory.getInstance().ShowOKAlertDialog(UIRegister.this, getString(R.string.dialog_tips), getString(R.string.login_register_success), new DialogInterface.OnClickListener()
				  {
				     @Override
				     public void onClick(DialogInterface dialog, int which)
				     {
				  	   dialog.dismiss();
				  	   finish();
				     }
				  });
               }
               else 
			   {
            	   UtiDialogsFactory.getInstance().ShowToastInfoShort(UIRegister.this, data.getHead().GetFieldValue("RspMsg"));
			   }
			}
			else
			{
			   UtiDialogsFactory.getInstance().ShowToastInfoShort(UIRegister.this, getString(R.string.dialog_server_failed));
			}
		 }
		 catch (Exception e)
		 {
			if (Session.getInstance().IsDEBUG())
			{
			   Log.e("ERROR", "UIRegister_onAsyncTaskEnd(taskId, taskName, params, result) ", e);
			   UtiDialogsFactory.getInstance().ShowToastInfoShort(UIRegister.this, getString(R.string.login_register_fail));
			}
		 }
	  }
   }
}
